package com.longcai.cm.controller;

import cn.hutool.core.date.DateUtil;
import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.domain.FaMember;
import com.longcai.cm.excel.ExcelUtils;
import com.longcai.cm.service.FaMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author LBX
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/FaMember")
public class FaMemberController {

    private final FaMemberService faMemberService;

    public FaMemberController(FaMemberService faMemberService) {
        this.faMemberService = faMemberService;
    }

    @RequestMapping("/getList")
    public R<PageVo<FaMember>> getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        if (pageInfo.getPageSearch()!=null){
            if (pageInfo.getPageSearch().get("SEARCH_LE_createtime")!=null) {
                String LE = (String) pageInfo.getPageSearch().get("SEARCH_LE_createtime");
                if (StringUtils.isNotBlank(LE)){
                    long l = DateUtil.parse(LE).getTime() / 1000L;
                    pageInfo.getPageSearch().put("SEARCH_LE_createtime", l);
                }
            }
            if (pageInfo.getPageSearch().get("SEARCH_GE_createtime")!=null){
                String GE = (String) pageInfo.getPageSearch().get("SEARCH_GE_createtime");
                if (StringUtils.isNotBlank(GE)){
                    long g = DateUtil.parse(GE).getTime() / 1000L;
                    pageInfo.getPageSearch().put("SEARCH_GE_createtime",g);
                }
            }
        }
        PageVo<FaMember> result = faMemberService.getPageBean(pageInfo, FaMember.class);
        return R.success(result);
    }

    /**
     * @param vo 插入实体
     * @return R
     */
    @Log
    @RequestMapping("/save")
    public R<String> save(FaMember vo) {
        boolean success = faMemberService.saveOrUpdate(vo);
        return success ? R.success() : R.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return R
     */
    @Log
    @RequestMapping("/edit")
    public R<String> edit(FaMember vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            boolean success = faMemberService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            return R.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return R
     */
    @Log
    @RequestMapping("/delete/{id}")
    public R<String> deleteByPrimaryKey(@PathVariable("id") Integer id) {
        final boolean success = faMemberService.removeById(id);
        return success ? R.success() : R.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return R
     */
    @Log
    @RequestMapping("/delete")
    public R<String> delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            final boolean success = faMemberService.removeById(idString);
            if (!success) {
                return R.error();
            }
        }
        return R.success();
    }


    //导出用户推广人数据
    @RequestMapping("/forExamPle")
    public void forExample(HttpServletRequest request, HttpServletResponse response, String id) {
        String name = faMemberService.getById(id).getWxname();
        List<FaMember> list = faMemberService.lambdaQuery().eq(FaMember::getPid, id).list();
        ExcelUtils.export(response,"用户"+name+"的推广人",list,FaMember.class);


    }
}
